const app = getApp();
const db = wx.cloud.database();
const _ = db.command;
Page({
  data: {
    active: 'home',
    loading: true,
    userInfo: app.globalData.userInfo,
    adminName: '',
    isAdmin: app.globalData.userInfo.type === 1,
    bindCheck: false,
  },
  onChange(event) {
    this.setData({
      active: event.detail
    });
  },
  onLoad() {
    const that = this;
    if (app.globalData.userInfo && app.globalData.userInfo.nickName) {
      db.collection('Management_user').where({
        _id: _.eq(app.globalData.userInfo.selectAdmin),
      }).get().then(res => {
        that.setData({
          adminName: res.data[0]?.nickName || ''
        })
      })
      // console.log(0, app.globalData.userInfo._id)
      db.collection('Management_user').doc(app.globalData.userInfo._id)
      .get().then(res => {
        // console.log(111, res.data)
        that.setData({
          bindCheck: !res.data?.userListCheck && that.data.adminName !== ''
        })
      })
      this.setData({
        loading: false,
      })
    } else {
      wx.redirectTo({
        url: '/pages/login/index',
      })
    }
  },
  onLogout(e) {
    wx.showToast({
      title: '退出登录！',
      icon: 'success'
    })
    wx.setStorageSync('userInfo', '{}');
    wx.redirectTo({
      url: '/pages/login/index',
    })
  }
});